<template>
    <div class="ranking">

        <div class="top">
            <div class="select">
                <div class="tab">
                    <ly-tab v-model="selectedId" :items="items" :options="options" @change="handleChange"></ly-tab>
                </div>
                <div class="time" @click="showTime = true">
                    <span v-if="selectedId == 0">{{date/1000 | time2}}</span>
                    <span v-if="selectedId == 1">{{date/1000 | time3}}</span>
                    <i class="iconfont iconxuexi_icon_xiala"></i>
                </div>
            </div>

            <div class="rank">
                <div class="boxTop" v-for="item of topData" :key="item.id">
                    <div class="img">
                        <img :src="item.photo" alt>
                        <div
                                class="no"
                                :class="item.lv =='1'? 'no1':item.lv==2?'no2':item.lv==3?'no3':'no1'"
                        >{{item.lv}}
                        </div>
                    </div>
                    <span>{{item.name}}</span>
                    <span>{{item.org_name}}</span>
                    <div class="fraction">
                        <span>{{item.grade_num}}</span>
                        <span>分</span>
                    </div>
                </div>
            </div>
        </div>
        <Loading v-if="notMore" class="empty-text-wrap"/>
        <div class="tabbox">
            <div class="content">
                <div class="box" v-for="item of listData" :key="item.id">
                    <div class="left">
                        <span class="no">{{item.lv}}.</span>
                        <img :src="item.photo" alt>
                        <div>
                            <p>{{item.name}}</p>
                            <p>{{item.org_name}}</p>
                        </div>
                    </div>

                    <div class="right">
                        <span>{{item.grade_num}}</span>
                        <span>分</span>
                    </div>
                </div>
            </div>
        </div>

        <mu-paper :z-depth="1" class="demo-date-picker" v-if="showTime && selectedId== 0 ">
            <div class="shadow" @click="showTime = false"></div>
            <mu-date-picker style="z-index:999" type="month" :date.sync="date" @change="changeTime"></mu-date-picker>
        </mu-paper>

        <mu-paper :z-depth="1" class="demo-date-picker" v-if="showTime && selectedId== 1 ">
            <div class="shadow" @click="showTime = false"></div>
            <mu-date-picker style="z-index:999" type="year" :date.sync="date" @change="changeTime"></mu-date-picker>
        </mu-paper>
    </div>
</template>

<script>
    import LyTab from "@cp/index";
    import Loading from '@/components/emptyText.vue';

    export default {
        components: {
            LyTab,
            Loading
        },
        data() {
            return {
                add: 1,
                selectedId: 0,
                showTime: false,
                date: new Date(),
                notMore: false,
                listData: [],
                topData: [],
                options: {
                    activeColor: "#fff",
                    color: "rgba(255,255,255,.6)"
                },
                items: [{label: "月榜"}, {label: "年榜"}]
            };
        },
        mounted() {
            this.$progress.done();
            this.init();
        },
        methods: {
            init(state, status, census_time) {
                this.notMore = false;
                this.$http
                    .get("/applets/StudyPlay/study_rank", {
                        state: this.add,
                        status: this.selectedId + 1,
                        type: 1,
                        phone: JSON.parse(Cookies.get('user_71ydj')).phone,
                        org_id: JSON.parse(
                            Cookies.get('user_71ydj')
                        ).organization_id,
                        census_time:
                        census_time ||
                        Math.floor(this.date / 1000) ||
                        Math.floor(new Date().getTime() / 1000)
                    })
                    .then(res => {
                        if (res.data.rank.length) {
                            this.listData = res.data.rank.slice(3);
                            this.topData = res.data.rank.slice(0, 3);
                        } else {
                            this.notMore = true;
                        }
                    });
            },
            addClass(i) {
                this.add = i;
                this.init();
            },

            changeTime(e) {
                this.date == e;
                this.init(null, null, Math.floor(e / 1000));
                this.showTime = false;
            },

            initData(p, class_id, type) {
                this.$http
                    .get("/applets/Study/audio_list", {
                        p: p,
                        page: 10,
                        class_id: class_id,
                        type: type
                    })
                    .then(res => {
                        this.listData = res.data;
                    });
            },
            handleChange(item, index) {
                this.init(null, index + 1);
            }
        }
    };
</script>

<style scoped lang="scss">
    * {
        margin: 0;
        padding: 0;
    }

    /deep/ .ly-tab-item-label {
        span {
            font-size: 20px;
        }
    }

    .demo-date-picker {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background: rgba(0, 0, 0, 0.3);
        display: flex;
        justify-content: center;
        align-items: center;
    }

    .top {
        width: 100%;
        height: 285px;
        background: url("~@as/img/xuexi_paihang_bg@2x.png");
        background-size: 100% 100%;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        .select {
            width: 100%;
            height: 50px;
            margin-top: 60px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            .tab {
                width: auto;
                margin-top: -10px;
            }
            .time {
                display: flex;
                align-items: center;
                width: auto;
                margin-right: 16px;
                i {
                    font-size: 12px;
                    color: rgba(255, 255, 255, 0.3);
                }
                span {
                    font-size: 16px;
                    color: #fff;
                    margin: 5px;
                }
            }
        }
    }

    .rank {
        width: 85%;
        height: 160px;
        display: flex;
        flex-direction: column;
        .boxTop {
            align-self: center;

            width: 100px;
            height: auto;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
            align-items: center;
            .img {
                position: relative;

                width: 55px;
                height: 55px;
                border-radius: 50%;
                img {
                    width: 55px;
                    height: 55px;
                    border-radius: 50%;
                }
                .no {
                    width: 18px;
                    height: 18px;
                    position: absolute;
                    bottom: 0;
                    right: 0;
                    color: #fff;
                    display: flex;
                    font-size: 10px;
                    justify-content: center;
                    align-items: center;
                }
                .no1 {
                    background: url("~@as/img/xuexi_paihang_1@2x.png");
                    background-size: 100% 100%;
                }

                .no2 {
                    background: url("~@as/img/xuexi_paihang_2@2x.png");
                    background-size: 100% 100%;
                }

                .no3 {
                    background: url("~@as/img/xuexi_paihang_3@2x.png");
                    background-size: 100% 100%;
                }
            }

            span {
                margin-top: 2px;
                font-size: 14px;
                color: #fff;
            }
            span + span {
                margin-top: 4px;
                color: rgba(255, 255, 255, 0.5);
                font-size: 10px;
                height: 14px;
                max-width: 100px;
                overflow: hidden;
                text-overflow: ellipsis;
                white-space: nowrap;
            }

            .fraction {
                height: 20px;
                display: flex;

                margin-top: 8px;
                span {
                    font-size: 16px;
                    color: #ffc63d;
                }
                span + span {
                    margin-left: 3px;
                    font-size: 12px;
                    align-self: flex-end;
                }
            }
        }

        .boxTop + .boxTop {
            align-self: flex-start;
            margin-top: -107px;
        }

        .boxTop + .boxTop + .boxTop {
            align-self: flex-end;
        }
    }

    .tabbox {
        width: 100%;
        height: auto;
        display: flex;
        align-items: center;
        flex-direction: column;
        .tab {
            width: 100%;
            display: flex;
            div {
                height: 44px;
                width: 50%;

                font-size: 16px;
                display: flex;
                align-items: center;
                justify-content: center;
                color: #f47548;
                font-weight: bold;
            }
        }
    }

    .selectColor {
        color: rgba(244, 117, 72, 0.3);
        font-weight: 400 !important;
        background: rgba(244, 117, 72, 0.2);
    }

    .content {
        width: 100%;
        .box {
            background: #fff;
            width: 100%;
            height: 67px;
            border-bottom: 1px solid #f8f8f8;
            display: flex;
            justify-content: space-between;
            align-items: center;
            .left {
                margin-left: 21px;
                display: flex;
                align-items: center;

                div {
                    margin-left: 15px;
                    height: 40px;
                    display: flex;
                    flex-direction: column;
                    justify-content: space-between;
                    p {
                        font-size: 14px;
                        color: #000;
                    }
                    p + p {
                        font-size: 12px;
                        color: #999;
                    }
                }
            }
            .no {
                font-size: 16px;
                color: #666;
            }
            img {
                width: 40px;
                height: 40px;
                margin-left: 25px;
                border-radius: 50%;
            }

            .right {
                margin-right: 21px;
                span {
                    font-size: 20px;
                    color: #ffc63d;
                }
                span + span {
                    margin-left: 3px;
                    font-size: 14px;
                }
            }
        }
    }

    .box:last-child {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        box-shadow: 1px 1px 5px 1px #888;
    }

    .shadow {
        width: 100%;
        height: 100%;
        position: absolute;
        top: 0;
        margin: 0;
        right: 0;
        bottom: 0;
        z-index: 1;
        background: rgba(0, 0, 0, 0.3);
    }

    .ranking .empty-text-wrap {
        width: 100%;
        height: 40vh;
        padding-top: 300px;
    }
</style>